//
//  ContainerView.m
//  宜居2
//
//  Created by 张金国-X on 16/3/8.
//  Copyright © 2016年 张金国-X. All rights reserved.
//

#import "ContainerView.h"
#import "Masonry.h"
@implementation ContainerView

#pragma mark - Setters
- (void)setRatio:(CGFloat)ratio {
    _ratio = ratio;
}
#pragma mark - Setters
- (void)setMargin:(CGFloat)margin {
    _margin = margin;
}
#pragma mark - Setters
-(void)setView:(UIView *)view{
    _view = view;
    _view.backgroundColor = [UIColor clearColor];
    [self addSubview:_view];
    self.backgroundColor = [UIColor clearColor];
}
/**
 *  @author X
 *
 *  设置内部容器View的边距
 *
 */
#pragma mark -Private
- (void)setupConstraintsForSubviews {

    if (self.margin > 0 && self.margin < self.frame.size.width/2) {
        
        [_view makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(self.margin);
            make.left.equalTo(self).offset(self.margin);
            make.right.equalTo(self).offset(-self.margin);
            make.bottom.equalTo(self).offset(-self.margin);
        }];
        
    }else {
        
        [_view makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.bottom.equalTo(self);
        }];
    }
}

#pragma mark Override
- (void)drawRect:(CGRect)rect {
    [self setupConstraintsForSubviews];
}
/**
 *  随机颜色
 *
 *  @return 返回一个随机颜色
 */
#pragma  mark Private
- (UIColor *)randomColor {
    CGFloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from black
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}

@end
